package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.video.*;
import com.wanxi.education.service.VideoService;
import com.wanxi.education.util.VideoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 寇文宝
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "后台视频管理")
@RestController
@RequestMapping("/back/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    //视频url前缀
    @Value("${domainName}")
    private String videoPath;

    //视频时长上限
    @Value("${videoTop}")
    private String videoTop;

    //视频时长下限
    @Value("${videoLower}")
    private String videoLower;

    @ApiOperation("视频管理页面分页查询+模糊查询")
    @PostMapping("/findPage")
    @ApiParam(name = "videoFuzzyQueryVo", value = "模糊查询vo类，分页参数，视频名称，视频uri")
    public CommonResult findPage(@RequestBody VideoFuzzyQueryVo videoFuzzyQueryVo) {
        PageInfo<VideoGoBackVo> videoGoBackVoPageInfo = videoService.findPage(videoFuzzyQueryVo);
        return CommonResult.success(videoGoBackVoPageInfo);
    }

    @ApiOperation("视频管理页面查看视频")
    @GetMapping("/findById")
    @ApiParam(name = "视频id", value = "当前想要查看视频的id")
    public CommonResult findById(@RequestParam("id") Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return CommonResult.validateFailed("视频id为空");
        }
        VideoFindByIdBackVo videoFindByIdBackVo = videoService.findById(id);
        return CommonResult.success(videoFindByIdBackVo);
    }

    @ApiOperation("视频管理页面新增视频")
    @PostMapping("/add")
    @ApiParam(name = "videoAddVo", value = "添加视频vo类")
    public CommonResult add(@RequestBody VideoAddVo videoAddVo, HttpSession session) {
        // 从session中获取到操作人id后装入videoAddVo中
        Long adminId = (Long) session.getAttribute("loginId");
        if (ObjectUtils.isEmpty(adminId)) {
            return CommonResult.unauthorized("未登录");
        }
        videoAddVo.setAdminId(adminId);
        try {
            //判断视频时长是否符合规范
            String time = VideoUtils.getDuration(videoPath + "/" + videoAddVo.getUri());
            //创建日期转换对象HH:mm:ss为时分秒
            DateFormat df = new SimpleDateFormat("HH:mm:ss");
            //将字符串转换为date类型
            Date parse = df.parse(time);
            //判断视频时长是否符合要求
            if (parse.getTime() > df.parse(videoTop).getTime()) {
                return CommonResult.failure("视频时长太长，添加失败,时长应在20-25分钟之内");
            }
            if (parse.getTime() < df.parse(videoLower).getTime()) {
                return CommonResult.failure("视频时长太短，添加失败,时长应在20-25分钟之内");
            }
            videoAddVo.setTime(time);
//            videoAddVo.setTime("00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("视频添加失败");
        }
        videoService.add(videoAddVo);
        return CommonResult.success("视频添加成功");
    }

    @ApiOperation("视频管理页面修改视频")
    @PostMapping("/update")
    @ApiParam(name = "videoUpdateVo", value = "修改视频vo类")
    public CommonResult update(@RequestBody VideoUpdateVo videoUpdateVo, HttpSession session) {
        // 从session中获取到操作人id后装入videoUpdateVo中
        Long adminId = (Long) session.getAttribute("loginId");
        if (ObjectUtils.isEmpty(adminId)) {
            return CommonResult.unauthorized("未登录");
        }
        videoUpdateVo.setAdminId(adminId);
        try {
            //判断视频时长是否符合规范
            String time = VideoUtils.getDuration(videoPath + "/" + videoUpdateVo.getUri());
            //创建日期转换对象HH:mm:ss为时分秒
            DateFormat df = new SimpleDateFormat("HH:mm:ss");
            //将字符串转换为date类型
            Date parse = df.parse(time);
            //判断视频时长是否符合要求
            if (parse.getTime() > df.parse(videoTop).getTime()) {
                return CommonResult.failure("视频时长太长，更新失败,时长应在20-25分钟之内");
            }
            if (parse.getTime() < df.parse(videoLower).getTime()) {
                return CommonResult.failure("视频时长太短，更新失败,时长应在20-25分钟之内");
            }
            videoUpdateVo.setTime(time);
//            videoUpdateVo.setTime("00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("视频更新失败");
        }
        videoService.update(videoUpdateVo);
        return CommonResult.success("更新成功");
    }

    @ApiOperation("视频管理页面删除视频")
    @PostMapping("/deleteById")
    @ApiParam(name = "videoDeleteVo", value = "删除视频资源vo类", required = true)
    public CommonResult deleteById(@RequestBody VideoDeleteVo videoDeleteVo,HttpSession session) {
        // 从session中获取到操作人id后装入videoDeleteVo中
        Long adminId = (Long) session.getAttribute("loginId");
        if (ObjectUtils.isEmpty(adminId)) {
            return CommonResult.unauthorized("未登录");
        }
        if (ObjectUtils.isEmpty(videoDeleteVo.getId())) {
            return CommonResult.validateFailed("视频id为空");
        }
        int i = videoService.deleteById(videoDeleteVo.getId(), adminId);
        if (i == 0) {
            return CommonResult.failure("当前视频正在被课程使用中，不能被删除");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("视频管理页面查看视频绑定")
    @PostMapping("/findVideoBindCourseById")
    @ApiParam(name = "findVideoBindCourseVo", value = "视频管理查看视频绑定哪些课程vo类", required = true)
    public CommonResult findVideoBindCourseById(@RequestBody FindVideoBindCourseVo findVideoBindCourseVo) {
        PageInfo<VideoBindCourseVo> videoBindCourseVoPageInfo = videoService.findVideoBindCourseById(findVideoBindCourseVo);
        return CommonResult.success(videoBindCourseVoPageInfo);
    }

}
